package com.ruoyi.personnel.oa.approve.controller;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.personnel.oa.approve.domain.ApproveDomain;
import com.ruoyi.personnel.oa.approve.domain.ApproveCond;
import com.ruoyi.personnel.oa.approve.domain.ApproveDto;
import com.ruoyi.personnel.oa.approve.service.IApproveService;
import com.ruoyi.produce.material.service.IProduceMaterialService;
import com.ruoyi.produce.process.planoutsource.service.IProducePlanOutsourceService;
import com.ruoyi.produce.process.planself.service.IProducePlanSelfService;
import com.ruoyi.produce.process.receiving.service.IReceivingReportService;
import com.ruoyi.produce.process.worksheet.service.IProduceWorkSheetService;
import com.ruoyi.psi.purchase.order.service.IPurchaseOrderService;
import com.ruoyi.psi.sale.delivery.service.ISaleDeliveryService;
import com.ruoyi.psi.sale.order.service.ISaleOrderService;
import com.ruoyi.psi.sale.salereturn.service.ISaleReturnService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视图层
 * 审批
 */
@RestController
@RequestMapping("/oa/approve")
@Api(value = "审批模块", tags = "审批接口")
public class ApproveController extends BaseController {
    @Autowired
    private IApproveService approveService;
    @Autowired
    private IPurchaseOrderService purchaseOrderService; //采购
    @Autowired
    private IReceivingReportService receivingReportService; //验收
    @Autowired
    private IProducePlanSelfService producePlanSelfService; //生产计划
    @Autowired
    private ISaleOrderService saleOrderService; //销售订单
    @Autowired
    private ISaleDeliveryService saleDeliveryService; //销售出库
    @Autowired
    private IProduceWorkSheetService workSheetService; //加工单
    @Autowired
    private IProduceMaterialService produceMaterialService; //领料单
    @Autowired
    private ISaleReturnService saleReturnService; //销售退回
    @Autowired
    private IProducePlanOutsourceService producePlanOutsourceService; //委外

    /**
     * 根据搜索条件所有审批记录
     * @param cond 搜索条件
     * @return
     */
    @ApiOperation(value = "获取审批列表",notes = "查询所有审批信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ApproveCond cond) {
        startPage();
        List<ApproveDto> approveList = approveService.selectApproveList(cond);
        return getDataTable(approveList);
    }
    /**
     * 根据搜索条件所有审批记录
     * @param  cond 搜索条件
     * @return
     */
    @ApiOperation(value = "根据roleId获取审批列表",notes = "查询所有审批信息")
    @PostMapping("/list1")
    public int list1(@RequestBody(required = false) ApproveCond cond) {
        int approveList = approveService.selectApproveByRoleId(cond);
        return approveList;
    }

    /**
     * 修改当前审批
     * @param approve
     * @return
     */
    @ApiOperation(value = "修改当前审批", notes = "修改当前审批")
    @ApiImplicitParam(name = "approve", value = "修改当前审批", dataType = "ApproveDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ApproveDomain approve) throws Exception {
        return toAjax(approveService.updateApprover(approve));
    }

    /**
     * 查询审批详情
     * @param approve
     * @return
     */
    @PostMapping("/details")
    @ApiOperation(value = "查询某审批记录的详情信息", notes = "查询某审批记录的详情信息")
    public AjaxResult selectDetails(@RequestBody ApproveDto approve) {
        String approveType = approve.getApproveType();
        Object obj = new Object();
        switch (approveType) {
            case ERPConstant.Approval_Type_Purchase: { //采购申请
                obj = purchaseOrderService.selectPurchaseOrderById(approve.getAssociationId());
                break;
            }
            case ERPConstant.Approval_Type_Storage: //验收入库
                obj = receivingReportService.selectReceivingReportById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_PRODUCE_PLAN: //生产计划审批
                obj = producePlanSelfService.selectProducePlanSelfById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_SALE_ORDER: //销售订单
                obj = saleOrderService.selectSaleOrderById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_SALE_DELIVERY: //销售订单出库
                obj = saleDeliveryService.selectSaleDeliveryById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_WORKSHEET: //加工单
                obj = workSheetService.selectProduceWorkSheetById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_PRODUCE_MATERIAL: //领料单
                obj = produceMaterialService.selectProduceMaterialRequisitionById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_SALE_RETURN: //销售退回
                obj = saleReturnService.selectSaleReturnById(approve.getAssociationId());
                break;
            case ERPConstant.APPROVAL_TYPE_PRODUCE_OUT_PLAN: //委外
                obj = producePlanOutsourceService.selectProducePlanOutsourceById(approve.getAssociationId());
                break;
        }
        return AjaxResult.success(obj);
    }

    /**
     * 审批拒绝
     * @param approve
     * @return
     */
    @ApiOperation(value = "审批拒绝", notes = "审批拒绝")
    @PostMapping("/refuse")
    public AjaxResult delete(@RequestBody ApproveDomain approve)
    {
        return toAjax(approveService.refuseApprove(approve));
    }

}
